<div class="sm-rightPanel pad-t-20">

    <span class="leftPanel-title-fixed">调查问卷列表</span>
    <span class="rightPanel-title-fixed">题目管理</span>

    <div class="sm-rightPanel-left">

        <div class="layui-form sm-operation">
            <div class="layui-form-item" id="tool_action_org">
                <button class="layui-btn layui-btn-normal" id="org_add">
                    <i class="iconfont icon-add"></i>添加
                </button>
                <button class="layui-btn layui-btn-warm" id="org_edit">
                    <i class="iconfont icon-bianji1"></i>修改
                </button>
                <button class="layui-btn layui-btn-danger" id="org_delete">
                    <i class="iconfont icon-delete"></i>删除
                </button>
            </div>
        </div>
        <ul id="ztree" class="ztree"></ul>
    </div>

    <div class="sm-rightPanel-right">
        <div class="sm-operation layui-form" id="list_form">
            <input type="hidden" name="level">
            <input type="hidden" name="paperInfoId">
            <blockquote class="site-text layui-elem-quote">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">类型：</label>
                        <div class="layui-input-inline">
                            <select id="questionTypeList" name="questionType" lay-filter="questionTypeList">
                                <option value="">全部</option>
                                <option value="1">单选</option>
                                <option value="2">多选</option>
                                <option value="3">判断</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">题目：</label>
                        <div class="layui-input-inline">
                            <input name="questionContent" type="text" autocomplete="off" placeholder=""
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" id="query"><i class="iconfont icon-sousuo"></i>查询</button>
                    </div>
                </div>
            </blockquote>


            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal" id="questions_add">
                    <i class="iconfont icon-add"></i>添加
                </button>
                <button class="layui-btn layui-btn-danger" id="questions_del">
                    <i class="iconfont icon-delete"></i>删除
                </button>
                <button class="layui-btn layui-btn-danger" id="questions_viewurl">
                    <i class="iconfont icon-delete"></i>查看链接
                </button>
            </div>
            <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
        </div>
    </div>

</div>



<script>
    layui.use(['layer', 'table', 'form',"layedit"], function () {
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
//        var layedit = layui.layedit;
        form.render();

//        var layeditIndex=null;
        var ztreeObj = null;
        var setting = {
            data: {
                key: {
                    title: "t"
                },
                simpleData: {
                    enable: true,
                }
            },
            async: {
                enable: true,
                url:"/paperInfo/getDataTree",
                dataFilter: ajaxDataFilter
            },
            view: {
                showIcon: true,
                showTitle: false,
                selectedMulti: false,
            },
            callback: {
                onClick: ztreeCallback,
            },
        };

        function ajaxDataFilter(treeId, parentNode, responseData) {
//            if (responseData) {
//                for(var i =0; i < responseData.length; i++) {
//                    responseData[i].name += "_filter";
//                }
//            }
            return responseData.data;
        };

        ztreeInit();


        //表格渲染
        var tableObj = table.render({
            id: 'list_table'
            , elem: '#list_table'
            , url: '/paperquestion/list'
            , align: "center"
            , cols: [[ //表头
                {type: 'checkbox'}
                , {type: 'numbers', title: '序号', width: 50}
                , {field: 'questionContent', title: '题目', width: 200, align: 'center'}
                ,{field: 'questionType', title: '类型', width: 150, align: 'center',templet: function (d) {
                    if("1" == d.questionType){
                        return "单选";
                    }
                    if("2" == d.questionType){
                        return "多选";
                    }
                    if("3" == d.questionType){
                        return "判断";
                    }
                }}
                , {field: 'createTime', title: '创建时间', width: 200, align: 'center'}
                , {field: 'updateTime', title: '修改时间', width: 200, align: 'center'}
                , {
                    field: 'lock', title: '操作', width: 100, templet: function (d) {
                        return '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>'
                    }, unresize: true, align: 'center'
                }
            ]], where: smartAui.getFormData("list_form")
        });

        //监听工具条
        table.on('tool(mainList)', function (obj) {
            var rowdata = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {

                smartAui.dialog({
                    title: '修改'
                    , template: 'addquestion'
                    , saveUrl: '/paperquestion/update'
                    , tableObj: tableObj
                    , width: 800
                    , height: 600
                    ,contentType:'application/json'
                    , htmlData: rowdata
                    , beforeSubmit: function (d) {
                        console.log(d);
                        var data={};
                        var type=d.questionType;
                        var questionContent=d.questionContent;
                        if(questionContent==""){
                            smartAui.alert("请输入题目");
                            return false;
                        }
                        data.questionType=type;
                        data.questionContent=questionContent;
                        data.option=[];
                        if(type=="1"){
                            var $options=$(".question-option-list>.item");
                            $options.each(function (_,v) {
                                var $item=$(v);
                                var text=$item.find("input[type=text]").val();
                                var id=$item.find("input[type=text]").attr("data-id");
                                data.option.push({
                                    optionContent:text,
                                    id:id,
                                    paperQuestionId:rowdata.id
                                });
                            });
                        }else if(type=="2"){
                            var $options=$(".question-option-list>.item");
                            $options.each(function (_,v) {
                                var $item=$(v);
                                var text=$item.find("input[type=text]").val();
                                var id=$item.find("input[type=text]").attr("data-id");
                                data.option.push({
                                    optionContent:text,
                                    id:id,
                                    paperQuestionId:rowdata.id
                                });
                            });
                        }else{
                            var id=$("#dialog_form").find("input[name=answer]").eq(0).attr("data-id");
                            var $options=$(".question-option-list input[name=answer]");
                            $options.each(function (_,v) {
                                var $item=$(v);
                                data.option.push({
                                    optionContent:$item.attr("title"),
                                    id:$item.attr("data-id"),
                                    paperQuestionId:rowdata.id
                                });
                            });
                        }

                        data.id = rowdata.id;

                        console.log(data);
                        var jsonData = JSON.stringify(data);
                        console.log(jsonData);

                        return jsonData;
                        //return data;
                    },
                    end: function (layero, index) {
                        console.log("===end===");

                        var paperInfoId = $("#list_form input[name=paperInfoId]").val();
                        smartAui.reloadTable(tableObj,{"paperInfoId":paperInfoId});
                    }
                })
            }
        });

        //添加
        $("#questions_add").click(function () {

            var nodes=ztreeObj.getSelectedNodes();
            if(nodes.length == 0){
                layer.msg("请选择节点");
                return;
            }
            var node = nodes[0];
            if(node.level==0){
                layer.msg("请选择父节点");
                return;
            }

            smartAui.dialog({
                title: '添加测试题'
                , template: 'addquestion'
                , saveUrl: '/paperquestion/save'
                , htmlData: {}
                , tableObj: tableObj
                , height: 600
                , contentType:'application/json;charset=utf-8'
                , beforeSubmit: function (d) {
                    console.log(d);
                    var data={};
                    var type=d.questionType;
                    var questionContent=d.questionContent;
                    if(questionContent==""){
                        smartAui.alert("请输入题目");
                        return false;
                    }
                    data.paperInfoId=$("#list_form input[name=paperInfoId]").val();
                    data.questionType=type;
                    data.questionContent=questionContent;
                    data.option=[];

                    if(type=="1"){
                        var $options=$(".question-option-list>.item");
                        $options.each(function (_,v) {
                            var $item=$(v);
                            var text=$item.find("input[type=text]").val();
                            data.option.push({
                                optionContent:text
                            });
                        });
                    }else if(type=="2"){
                        var $options=$(".question-option-list>.item");
                        $options.each(function (_,v) {
                            var $item=$(v);
                            var text=$item.find("input[type=text]").val();
                            data.option.push({
                                optionContent:text
                            });
                        });
                    }else{
                        var id=$("#dialog_form").find("input[name=answer]").eq(0).attr("data-id");
                        var $options=$(".question-option-list input[name=answer]");
                        $options.each(function (_,v) {
                            var $item=$(v);

                            data.option.push({
                                optionContent:$item.attr("title"),
                            });
                        });
                    }

                    console.log(data);
                    var jsonData = JSON.stringify(data);
                    console.log(jsonData);

                    return jsonData;

                    //return data;
                },
                end: function (layero, index) {
                    console.log("===end===");

                    var paperInfoId = $("#list_form input[name=paperInfoId]").val();
                    smartAui.reloadTable(tableObj,{"paperInfoId":paperInfoId});
                }
            })
        });

        //删除
        $("#questions_del").click(function () {
            var data = table.checkStatus('list_table').data;//已选中数据
            if (data.length == 0) {
                layer.msg("请至少选择一条");
                return;
            }
            var arr = [];     //选中数组
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].id)
            }
            smartAui.confirm('确认删除？', function () {
                $.ajax({
                    type: "GET",
                    url: '/paperquestion/delete',
                    data: {"id": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");

                            var paperInfoId = $("#list_form input[name=paperInfoId]").val();
                            smartAui.reloadTable(tableObj,{"paperInfoId":paperInfoId});
                        }
                    }
                })
            }, 0);
        });


        //查看链接
        $("#questions_viewurl").click(function () {

            var nodes=ztreeObj.getSelectedNodes();
            if(nodes.length == 0){
                layer.msg("请选择节点");
                return;
            }
            var node = nodes[0];
            if(node.level==0){
                layer.msg("请选择父节点");
                return;
            }

            var paperInfoId = $("#list_form input[name=paperInfoId]").val();

            smartAui.dialog({
                title: '报名链接'
                , template: 'viewurl'
                , saveUrl: ''
                , tableObj: tableObj
                , width: 850
                , height: 600
                , htmlData: {"id":paperInfoId}
                ,yes:function(){
                    layer.closeAll();
                }
            })
        });

        $("#query").click(function() {

            var paperInfoId = $("#list_form input[name=paperInfoId]").val();
            var questionType = $("#questionTypeList").val();
            smartAui.reloadTable(tableObj,{"paperInfoId":paperInfoId,"questionContent":$("input[name='questionContent']").val(),"questionType":questionType});
        });


        function ztreeInit() {
            $.ajax({
                url: '/paperInfo/getDataTree',
                data: {},
                success: function (result) {
                    if (result && result.data) {
                        ztreeObj=smartAui.initZtree("ztree", setting, result.data);
                    }else{
                        layer.msg("数据异常");
                    }
                },
            });
        }


        function ztreeCallback(event, treeId, treeNode) {

            // 清空搜索框内容
            $("input[name='questionContent']").val("");

            var arr = ztreeObj.getSelectedNodes();
            if (arr.length == 0) {
                return;
            }
            var node = arr[0];
            console.log(node);

            var id = node.id;
            var level = node.level;
            $("#list_form input[name=paperInfoId]").val(id);
            $("#list_form input[name=level]").val(level);

            smartAui.reloadTable(tableObj);
        }













        // 顶部 [添加]和[删除]操作事件
        $("#org_add").click(function () {
            var nodes=ztreeObj.getSelectedNodes();
            if(nodes.length == 0){
                layer.msg("请选择节点");
                return;
            }
//            var node = nodes[0];
//            if(node.level!=0){
//                layer.msg("只能在根节点上添加");
//                return;
//            }

            smartAui.dialog({
                title: '添加'
                , template: 'add'
                , saveUrl: '/paperInfo/save'
                , tableObj: tableObj
                , width: 800
                , height: 600
                , submited:function () {
                    ztreeInit();
                }
            });


        });

        //修改
        $("#org_edit").click(function () {
            var nodes=ztreeObj.getSelectedNodes();
            if(nodes.length==0){
                layer.msg("请选择节点");
                return;
            }
            var node=nodes[0];
            if(node.level==0){
                layer.msg("根节点不可修改");
                return;
            }

            $.ajax({
                url: '/paperInfo/info?id='+node.id,
                contentType: 'application/json',
                success: function (result) {
                    if(result && result.data){
                        smartAui.dialog({
                            title: '修改'
                            , template: 'edit'
                            , saveUrl: '/paperInfo/save'
                            , tableObj: tableObj
                            , width: 800
                            , height: 600
                            , htmlData: result.data
                            ,submited:function () {
                                ztreeInit();
                            }
                        });
                    }
                },
                error: function () {
                    layer.msg("操作异常");
                }
            });


        });

        $("#org_delete").click(function () {
            var nodes=ztreeObj.getSelectedNodes();
            if(nodes.length==0){
                layer.msg("请选择节点");
                return;
            }
            var node=nodes[0];
            if(node.level==0){
                layer.msg("根节点不可删除");
                return;
            }
            if(node.isParent){
                layer.msg("请先删除子节点");
                return;
            }
            var obj = {"id": node.id};
            smartAui.confirm('确认删除？',function () {
                $.ajax({
                    type: "GET",
                    url: '/paperInfo/delete',
                    data: obj,
                    success: function (result) {
                        if (result.success) {
                            layer.msg(result.msg || "删除成功");
                            smartAui.reloadTable(tableObj);
                            ztreeInit();
                        }else{
                            layer.msg( result.msg || "删除异常");
                        }
                    },
                    error: function () {
                        layer.msg("删除异常");
                    }
                })
            },0);
        });




    });
</script>